package com.example.home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.home.R
import com.example.home.data.FingerRvItem

class FingerShapeRvAdapter(
    private val items: List<FingerRvItem>,
) : RecyclerView.Adapter<FingerShapeRvAdapter.FingerShapeViewHolder>() {

    var onClickListener: ((item: FingerRvItem) -> Unit)? = null

    class FingerShapeViewHolder(private val binding: com.example.home.databinding.ItemRvFingerShapeBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(item: FingerRvItem, onClickListener: ((item: FingerRvItem) -> Unit)?) {
            binding.rvItemTv.text = item.text
            binding.root.setOnClickListener {
                onClickListener?.invoke(item)
            }
            binding.executePendingBindings()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FingerShapeViewHolder {
        val binding = DataBindingUtil.inflate<com.example.home.databinding.ItemRvFingerShapeBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_rv_finger_shape,
            parent,
            false
        )
        return FingerShapeViewHolder(binding)
    }

    override fun onBindViewHolder(holder: FingerShapeViewHolder, position: Int) {
        holder.bind(items[position], onClickListener)
    }

    override fun getItemCount(): Int = items.size
}
